import request from '@/utils/request.js'
export function queryComment(data) {
    return request({
        url: '/comment/query',
        method: 'get',
        params: data
    })
}
export function addComment(data) {
    return request({
        url: '/comment/add',
        method: 'post',
        data:data
    })
}
export function delComment(id) {
    return request({
        url: '/comment/'+id,
        method: 'delete'
    })
}
export function editComment(data) {
    return request({
        url: '/comment/edit',
        method: 'put',
        data:data
    })
}
export function selComment(id) {
    return request({
        url: '/comment/'+id,
        method: 'get',
    })
}
export function auditComment(data) {
    return request({
        url: '/comment/audit',
        method: 'put',
        data:data
    })
}
export function queryTreeComment(params) { 
    return new Promise((resolve,reject) => {
        request({
            url: '/comment/query',
            method: 'get',
            params: params 
        }).then(response=>{
            console.log(params)
            let data = response.data.list
            var tree = [], temp = {}
            data.forEach((item) =>{
                temp[item['id']] = item  // 以id作为索引存储元素，可以无需遍历直接定位元素
            })
            data.forEach((item) =>{
                let currentElement = item
                let tempCurrentElementParent = temp[currentElement['pid']]  // 临时变量里面的当前元素的父元素
                if(tempCurrentElementParent){
                    if(!tempCurrentElementParent['children']){         // 如果父元素没有chindren键
                        tempCurrentElementParent['children'] = []      // 设上父元素的children键
                    }
                    tempCurrentElementParent['children'].push(currentElement)   // 给父元素加上当前元素作为子元素
                } else {
                    // 不存在父元素，意味着当前元素是一级元素
                    tree.push(currentElement)
                }
            })
            response.data = tree
            resolve(response)
        })
    }).catch(error=>{
        reject(error)}
    )
}